package com.elecbook.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elecbook.base.dto.PageDTO;
import com.elecbook.system.mapper.PropertiesMapper;
import com.elecbook.system.model.dto.PropertiesBatchDTO;
import com.elecbook.system.model.dto.PropertiesDTO;
import com.elecbook.system.model.dto.PropertiesPQuery;
import com.elecbook.system.model.po.Properties;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Z-ken
 * @since 2024/11/11 16:54:42
 */
@Service
public class PropertiesServiceImpl extends ServiceImpl<PropertiesMapper, Properties> implements PropertiesService {


    @Override
    public String getValue(PropertiesDTO dto) {
        LambdaQueryWrapper<Properties> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Properties::getName, dto.getKey());
        Properties properties = baseMapper.selectOne(wrapper);
        return properties.getValue();
    }

    @Override
    public List<Properties> getProperties(PropertiesBatchDTO propertiesBatchDTO) {
        List<Properties> properties = baseMapper.selectPropertiesList(propertiesBatchDTO);
        return properties;
    }
    
    /**
     * 基础分页请求--管理员
     */
    @Override
    public PageDTO<Properties> pageQuery(PropertiesPQuery query) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则-这里为了方便扩展和使用先进行创建
        List<OrderItem> orderItems = query.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<Properties> page = query.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
    /*
    查询
    */
        Page<Properties> p = baseMapper.selectPageEntity(page, query);

        // 获取数据信息
        return PageDTO.of(p, Properties.class);
    }


    @Override
    public Properties saveProperties(Properties properties) {
        properties.setId(null);
        baseMapper.insert(properties);
        return properties;
    }

    @Override
    public Properties updatePropertiesById(Properties properties) {
        baseMapper.updateById(properties);
        return properties;
    }

}
